package docbookarchivemanager.converter.elements;

import docbookarchivemanager.utils.DomManager;
import docbookarchivemanager.utils.NamespaceContextImpl;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Jakub Hrachovec
 */
public class Data {

    private static ArrayList<OutputFolder> xpathToSearch = new ArrayList<>();
    private static ArrayList<OutputFolder> xpathToSearch_xsl = new ArrayList<>();

    public static void prepareData() {
        NamespaceContextImpl.bindDefaultNamespaces();
        xpathToSearch = prepare("elements.xml");
        xpathToSearch_xsl = prepare("elements_xsl.xml");

    }

    private static ArrayList<OutputFolder> prepare(String nameOfFile) {
        //Parses data from the given XML and creates a data structure collection.
        ArrayList<OutputFolder> outputFolders = new ArrayList<>();

        Document doc = DomManager.readDom(Data.class.getResourceAsStream(nameOfFile));

        if (doc != null) {
            NodeList nodes = DomManager.evaluateXpath(doc, "/darElements:list/darElements:outputFolders/darElements:outputFolder");

            if (nodes != null) {
                for (int i = 0; i < nodes.getLength(); i++) {
                    Node curNode = nodes.item(i);
                    OutputFolder outputFolder = new OutputFolder(curNode.getAttributes().getNamedItem("name").getNodeValue().trim());

                    for (Node ele : DomManager.getOnlyElements((DomManager.getOnlyElements(curNode.getChildNodes())).get(0).getChildNodes())) {
                        NamedNodeMap eleAtts = ele.getAttributes();
                        String nameOfElement = eleAtts.getNamedItem("name").getNodeValue().trim();
                        String namespaceOfElement = eleAtts.getNamedItem("namespace").getNodeValue().trim();
                        String namespacePrefixOfElement = NamespaceContextImpl.getNamespacePrefix(namespaceOfElement);
                        Element element = new Element(nameOfElement);
                        element.setNamespace(namespaceOfElement);
                        element.setNamespace_prefix(namespacePrefixOfElement);

                        for (Node att : DomManager.getOnlyElements((DomManager.getOnlyElements(ele.getChildNodes())).get(0).getChildNodes())) {
                            element.addAttribute(new Attribute(att.getAttributes().getNamedItem("name").getNodeValue().trim()));
                        }
                        NamespaceContextImpl.bindPrefixToNamespaceURI(namespacePrefixOfElement, namespaceOfElement);

                        outputFolder.addElement(element);
                    }
                    outputFolders.add(outputFolder);
                }
            }
        }
        return outputFolders;
    }

    /**
     *
     * @return list of elements and their attributes to search for in a Docbook
     * XML.
     */
    public static ArrayList<OutputFolder> getDataToSearch() {
        return xpathToSearch;
    }

    /**
     *
     * @return list of elements and their attributes to search for in an XSL.
     */
    public static ArrayList<OutputFolder> getDataToSearch_xsl() {
        return xpathToSearch_xsl;
    }
}
